package logic

import (
	"fmt"
	"gctl/pkg"
	"os"
	"path/filepath"
)

func AppInit(app string, srv string) {
	//pkg.ChEndFileName("/Users/hyx/work/gowork/gospace/src/gctl/tpl/pkg")

	//遍历srv模板目录
	curPath, _ := os.Getwd()
	//pkg
	pkg.Render(app, "pkg", "pkg")
	//srv
	pkg.Render(app, srv, "srv")
	//config
	pkg.CopyTo(filepath.Join(curPath, "tpl/srv/cmd/apps/config.yaml"), filepath.Join(curPath, app, srv, "/cmd/apps/config.yaml"))
	fmt.Println("config路径 tpl：", filepath.Join(curPath, "tpl/srv/cmd/apps/config.yaml"))
	fmt.Println("config路径 to：", filepath.Join(curPath, app, srv, "/cmd/apps/config.yaml"))
	//gitignore
	pkg.CopyTo(curPath+"/tpl/common/.gitignore", curPath+"/"+app+"/.gitignore")
}

func AppCreate(projectRoot string) error {
	// 创建cmd目录及子目录和文件
	cmdDir := filepath.Join(projectRoot, "cmd")
	err := createDir(cmdDir)
	if err != nil {
		return err
	}
	appsDir := filepath.Join(cmdDir, "apps")
	err = createDir(appsDir)
	if err != nil {
		return err
	}
	nacosDir := filepath.Join(appsDir, "nacos")
	err = createDir(nacosDir)
	if err != nil {
		return err
	}
	// 创建internal目录及子目录
	internalDir := filepath.Join(projectRoot, "internal")
	err = createDir(internalDir)
	if err != nil {
		return err
	}
	logicDir := filepath.Join(internalDir, "logic")
	err = createDir(logicDir)
	if err != nil {
		return err
	}
	modelDir := filepath.Join(internalDir, "model")
	err = createDir(modelDir)
	if err != nil {
		return err
	}
	daoDir := filepath.Join(modelDir, "dao")
	err = createDir(daoDir)
	if err != nil {
		return err
	}
	dtoDir := filepath.Join(modelDir, "dto")
	err = createDir(dtoDir)
	if err != nil {
		return err
	}
	poDir := filepath.Join(modelDir, "po")
	err = createDir(poDir)
	if err != nil {
		return err
	}
	return nil
}

// 创建目录
func createDir(dirPath string) error {
	return os.MkdirAll(dirPath, 0755)
}
